from flask_login import UserMixin
import hashlib
import base64
import os
from app import db, login_manager

@login_manager.user_loader
def load_user(doctor_id):
    return Doctor.query.get(doctor_id)

# 定义所有模型
class Patient(db.Model):
    __tablename__ = 'patient'
    patient_id = db.Column(db.String(20), primary_key=True)
    patient_name = db.Column(db.String(20))
    gender = db.Column(db.String(3))
    age = db.Column(db.Integer)
    cases = db.relationship('Case', backref=db.backref('patient_info', lazy=True))
    takes = db.relationship('Takes', backref=db.backref('patient_info', lazy=True))


class Doctor(UserMixin, db.Model):
    __tablename__ = 'hospital_user'
    doctor_id = db.Column(db.String(20), primary_key=True)
    doctor_name = db.Column(db.String(20))
    password = db.Column(db.String(128))
    salt = db.Column(db.String(32))
    type = db.Column(db.String(15))
    dept_name = db.Column(db.String(20))
    ward_id = db.Column(db.String(20))

    def get_id(self):
        return self.doctor_id

    def set_password(self, password):
        self.salt = base64.b64encode(os.urandom(16)).decode('utf-8')
        password_hash = hashlib.sha256((password + self.salt).encode('utf-8')).hexdigest()
        self.password = password_hash

    def check_password(self, password):
        password_hash = hashlib.sha256((password + self.salt).encode('utf-8')).hexdigest()
        return self.password == password_hash

    cases = db.relationship('Case', backref=db.backref('doctor_info', lazy=True))
    beds = db.relationship('Bed', backref=db.backref('doctor_info', lazy=True))


class Case(db.Model):
    __tablename__ = 'cases'
    case_id = db.Column(db.String(20), primary_key=True)
    patient_id = db.Column(db.String(20), db.ForeignKey('patient.patient_id'))
    doctor_id = db.Column(db.String(20), db.ForeignKey('hospital_user.doctor_id'))
    dept_name = db.Column(db.String(20))
    description = db.Column(db.String(30))
    predict_days = db.Column(db.Numeric(4, 2))


class Bed(db.Model):
    __tablename__ = 'bed'
    bed_id = db.Column(db.String(20), primary_key=True)
    ward_id = db.Column(db.String(20), db.ForeignKey('ward.ward_id'))
    used_status = db.Column(db.Integer)
    doctor_id = db.Column(db.String(20), db.ForeignKey('hospital_user.doctor_id'))


class Ward(db.Model):
    __tablename__ = 'ward'
    ward_id = db.Column(db.String(20), primary_key=True)
    dept_name = db.Column(db.String(20))
    bed_total = db.Column(db.Integer)
    bed_used = db.Column(db.Integer)
    avg_time = db.Column(db.Numeric(4, 2))
    location = db.Column(db.String(30))

    beds = db.relationship('Bed', backref=db.backref('ward_info', lazy=True))


class Department(db.Model):
    __tablename__ = 'department'
    dept_name = db.Column(db.String(20), primary_key=True)
    bed_total = db.Column(db.Integer)
    bed_used = db.Column(db.Integer)


class Takes(db.Model):
    __tablename__ = 'takes'
    patient_id = db.Column(db.String(20), db.ForeignKey('patient.patient_id'), primary_key=True)
    bed_id = db.Column(db.String(20), db.ForeignKey('bed.bed_id'), primary_key=True)
    case_id = db.Column(db.String(10), db.ForeignKey('cases.case_id'))
    in_date = db.Column(db.DateTime)
    out_date = db.Column(db.DateTime)

    case = db.relationship('Case', backref=db.backref('takes_info', lazy=True))


class Request(db.Model):
    __tablename__ = 'request'
    request_id = db.Column(db.String(20), primary_key=True)
    case_id = db.Column(db.String(20), db.ForeignKey('cases.case_id'))
    wanted_bed = db.Column(db.String(20))
    request_status = db.Column(db.Integer)

    case = db.relationship('Case', backref=db.backref('request_info', lazy=True))

    def to_string(self):
        print(f"request_id: {self.request_id}, case_id: {self.case_id}, wanted_bed: {self.wanted_bed}, request_status: {self.request_status}")


class Disease(db.Model):
    __tablename__ = 'disease'
    categories = db.Column(db.String(30))
    illness = db.Column(db.String(30), primary_key=True)
    avg_days = db.Column(db.Numeric(4, 2))
